<script setup lang="ts">
import type { SysUser } from '~/types/system/user'

const emit = defineEmits<{
  (e: 'onSearch', formModel: SysUser): void
}>()

const searchFormRef = ref()
const formModel = reactive<SysUser>({
  username: '',
  account: '',
  sex: undefined,
  email: '',
  mobile: '',
  state: undefined,
})
defineExpose({ formModel })

/** 表单搜索事件 */
function search() {
  return emit('onSearch', formModel)
}

/** 表单重置事件 */
function reset() {
  return searchFormRef.value.reset()
}
</script>

<template>
  <div class="z-search-layout">
    <lay-row space="8">
      <lay-col :md="22" :sm="20" :xs="24">
        <lay-form
          ref="searchFormRef"
          :model="formModel"
        >
          <lay-row>
            <lay-col :md="8" :sm="12" :xs="24">
              <lay-form-item label="用户名" prop="username">
                <lay-input v-model="formModel.username" placeholder="请填写" allow-clear />
              </lay-form-item>
            </lay-col>
            <lay-col :md="8" :sm="12" :xs="24">
              <lay-form-item label="账号" prop="account">
                <lay-input v-model="formModel.account" placeholder="请填写" allow-clear />
              </lay-form-item>
            </lay-col>
            <lay-col :md="8" :sm="12" :xs="24">
              <lay-form-item label="性别" prop="sex">
                <lay-select v-model="formModel.sex" placeholder="请选择" allow-clear style="width: 100%">
                  <lay-select-option value="0" label="未知" />
                  <lay-select-option value="1" label="男" />
                  <lay-select-option value="2" label="女" />
                </lay-select>
              </lay-form-item>
            </lay-col>
            <lay-col :md="8" :sm="12" :xs="24">
              <lay-form-item label="邮箱" prop="email">
                <lay-input v-model="formModel.email" placeholder="请填写" allow-clear />
              </lay-form-item>
            </lay-col>
            <lay-col :md="8" :sm="12" :xs="24">
              <lay-form-item label="手机号" prop="mobile">
                <lay-input v-model="formModel.mobile" placeholder="请填写" allow-clear />
              </lay-form-item>
            </lay-col>
            <lay-col :md="8" :sm="12" :xs="24">
              <lay-form-item label="状态" prop="state">
                <lay-select v-model="formModel.state" placeholder="请选择" allow-clear style="width: 100%">
                  <lay-select-option value="0" label="停用" />
                  <lay-select-option value="1" label="启用" />
                </lay-select>
              </lay-form-item>
            </lay-col>
          </lay-row>
        </lay-form>
      </lay-col>
      <lay-col :md="2" :sm="4" :xs="24" class="z-text-center">
        <lay-space :direction="isXs ? 'horizontal' : 'vertical'" wrap>
          <lay-button type="primary" @click="search">
            <lay-icon type="layui-icon-search" /> 查询
          </lay-button>
          <lay-button @click="reset">
            <lay-icon type="layui-icon-refresh-three" /> 重置
          </lay-button>
        </lay-space>
      </lay-col>
    </lay-row>
  </div>
</template>

<style scoped>
:deep(.layui-form-item) {
  margin-bottom: 10px;
}
</style>
